﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using IMLibrary.Net.Enum;

namespace IMLibrary.BinaryPacket
{
    /// <summary>
    /// 二进制文件传输消息
    /// </summary>
    public class BinaryFileMsg :BasePacket 
    {
        /// <summary>
        /// 基础包长度
        /// </summary>
        public static readonly int HeaderLength = 56;

        /// <summary>
        /// 构造函数（建议不要修改）
        /// </summary>
        public BinaryFileMsg()
            : base(HeaderLength)
        {
            
        } 

        /// <summary>
        /// 构造函数（建议不要修改）
        /// </summary>
        public BinaryFileMsg(FileServerCommandType commandType)
            : base(HeaderLength)
        { 
            this.CommandType = (byte)commandType;
        }

        /// <summary>
        /// 构造函数（建议不要修改）
        /// </summary>
        /// <param name="data"></param>
        public BinaryFileMsg(byte[] data)
            : base(data, HeaderLength)
        {

        }

        /// <summary>
        /// 命令类型(1字节)
        /// </summary>
        public byte CommandType
        {
            set
            {
                BaseData[0] = value;
            }
            get
            {
                return BaseData[0];
            }
        }
        /// <summary>
        /// 从字节数组获取消息发送者
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte GetCommandType(byte[] data)
        {
            if (data == null || data.Length < HeaderLength)
                return 0;
            else
                return data[0];
        } 

        public byte MD5Length
        { 
            get
            {
                return BaseData[1];
            }
        }

        public byte ExtensionLength
        { 
            get
            {
                return BaseData[2];
            }
        } 

        /// <summary>
        /// 传输文件的大小
        /// </summary>
        public long Length
        {
            set
            {
                Buffer.BlockCopy(BitConverter.GetBytes(value), 0, BaseData, 3, 8);
            }
            get
            {
                return BitConverter.ToInt64(BaseData, 3);
            }
        }

        /// <summary>
        /// 上次发送的文件位置
        /// </summary>
        public long LastLength
        {
            set
            {
                Buffer.BlockCopy(BitConverter.GetBytes(value), 0, BaseData, 11, 8);
            }
            get
            {
                return BitConverter.ToInt64(BaseData,11);
            }
        }

        /// <summary>
        /// 文件MD5值（32字节）
        /// </summary>
        public string MD5
        {
            set
            {
                if (string.IsNullOrEmpty(value)) return;
                if (value.Length == 16 || value.Length == 32)
                {
                    byte[] buf = Encoding.ASCII.GetBytes(value);
                    BaseData[1] = (byte)buf.Length;
                    Buffer.BlockCopy(buf, 0, BaseData, 19, buf.Length);
                }
            }
            get
            {
                var buf=new byte[MD5Length];
                Buffer.BlockCopy(BaseData, 19, buf, 0, buf.Length);
                return Encoding.ASCII.GetString(buf);
            }
        }

        byte[] data = Encoding.ASCII.GetBytes("12345");
        /// <summary>
        /// 文件扩展名（5字节）
        /// </summary>
        public string Extension
        {
            set
            {
                if (string.IsNullOrEmpty(value)) return;
                if (value.Length >5) return;
                byte[] buf = Encoding.ASCII.GetBytes(value);
                BaseData[2] = (byte)buf.Length;
                Buffer.BlockCopy(data, 0, BaseData, 19 + MD5Length, data.Length);
                Buffer.BlockCopy(buf, 0, BaseData, 19 + MD5Length, buf.Length);
            }
            get
            {
                var buf = new byte[ExtensionLength];
                Buffer.BlockCopy(BaseData, 19 + MD5Length, buf, 0, buf.Length);
                return Encoding.ASCII.GetString(buf);
            }
        }
    }
}
